{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "private_outputs": true,
      "provenance": [],
      "authorship_tag": "ABX9TyM4eXFR1NCnb34JtyEWFMKI",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/ktynski/Marketing_Automations_Notebooks_With_GPT/blob/main/Intents%2C_Personas%2C_and_Buyer_Inference_(Public).ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install openai pydot graphviz"
      ],
      "metadata": {
        "id": "t3VfEPL5GVLh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "import pandas as pd\n",
        "import re\n",
        "import time\n",
        "import concurrent.futures\n",
        "\n",
        "# set up OpenAI API credentials\n",
        "openai.api_key = \"Your API Key\"\n",
        "\n",
        "\n",
        "# Define the keyword\n",
        "keyword = \"Home Alarm Systems\"\n",
        "num_personas = 3\n",
        "\n",
        "# Use GPT3 to predict the intent of the keyword search\n",
        "prompt = f\"What is the most likely intent of searching for {keyword}?\"\n",
        "response = openai.Completion.create(\n",
        "  engine=\"text-davinci-003\",\n",
        "  prompt=prompt,\n",
        "  max_tokens=60,\n",
        "  n=1,\n",
        "  stop=None,\n",
        "  temperature=0.5\n",
        ")\n",
        "intent = response.choices[0].text.strip()\n",
        "\n",
        "# Generate 3 personas for the keyword/intent\n",
        "prompt = (f\"You are an expert at marketing and consumer behavior, specifically persona generation. Given the searcher is using the keyword: \\n {keyword} with the intent: \\n {intent}, please generate 3 different but unique personas with the following format. Each persona should be seperated by ######## : \\n\\n\"\n",
        "          \"1. Demographics\\n\"\n",
        "          \"- Name:\\n\"\n",
        "          \"- Age Range:\\n\"\n",
        "          \"- Gender:\\n\"\n",
        "          \"- Marital Status:\\n\"\n",
        "          \"- Income:\\n\"\n",
        "          \"- Education Level:\\n\\n\"\n",
        "          \"2. Psychographics\\n\"\n",
        "          \"- Personality Traits:\\n\"\n",
        "          \"- Values and Beliefs:\\n\"\n",
        "          \"- Interests and Hobbies:\\n\"\n",
        "          \"- Lifestyle Factors:\\n\\n\"\n",
        "          \"4. Behavior and Decision-Making\\n\"\n",
        "          \"- Information Sources:\\n\"\n",
        "          \"- Influences on Purchase Decisions:\\n\"\n",
        "          \"- Key Behaviors and Habits:\\n\\n\"\n",
        "          )\n",
        "response = openai.Completion.create(\n",
        "    engine=\"text-davinci-003\",\n",
        "    prompt=prompt,\n",
        "    max_tokens=2000,\n",
        "    n=1,\n",
        "    stop=None,\n",
        "    temperature=0.8\n",
        ")\n",
        "personas_text = response.choices[0].text.strip()\n",
        "\n",
        "# Parse the personas\n",
        "personas = personas_text.split('########')\n",
        "personas = [persona.strip() for persona in personas if persona.strip()]\n",
        "\n",
        "# For each persona, generate the responses to the questions\n",
        "results = []\n",
        "with concurrent.futures.ThreadPoolExecutor() as executor:\n",
        "    tasks = []\n",
        "    for persona in personas:\n",
        "        for question in [\n",
        "            f\"For the following question please answer with a list. What are the most important features or benefits that a given persona is looking for when searching for {keyword}, given a persona of: \\n Persona:\\n {persona} ?\",\n",
        "            f\"For the following question please answer with a list. What are the most common questions or concerns before making a purchase related to {keyword}, given a persona of: \\n Persona:\\n {persona} ?\",\n",
        "            f\"For the following question please answer with a list. What are the biggest challenges that someone faces when searching for the right product or service related to {keyword}, given a persona of: \\n Persona: \\n {persona} ?\",\n",
        "            f\"For the following question please answer with a list. How does the given persona typically research their options related to {keyword}, and where do they go to find information, given a persona of: \\n Persona: \\n {persona}?\",\n",
        "            f\"For the following question please answer with a list. What are the most effective ways to convince the following persona to make a purchase related to {keyword}, given a persona of: \\n Persona: \\n {persona} ?\",\n",
        "            f\"For the following question please answer with a list. What are the most common objections when considering a purchase related to {keyword}, and how can they be addressed given a persona of: \\n Persona: \\n {persona}\",\n",
        "            f\"For the following question please answer with a list. What are some unique or unconventional ways to market products or services related to {keyword} given a persona of: \\n Persona: \\n {persona} ?\",\n",
        "            f\"For the following question please answer with a list. How does the given persona typically compare different products or services related to {keyword}, and what factors do they consider? \\n Persona: \\n {persona}\",\n",
        "            f\"For the following question please answer with a list. What are the most common misconceptions or misunderstandings related to {keyword}, and how can they be corrected? given the persona of: \\n Persona: \\n {persona}\",\n",
        "            f\"For the following question please answer with a list. What are some emerging trends or developments in the market for {keyword}, and how can they leverage them to gain an advantage given the persona of: \\n Persona: \\n {persona}?\"\n",
        "        ]:\n",
        "            prompt = question\n",
        "            task = executor.submit(openai.Completion.create,\n",
        "              engine=\"text-davinci-003\",\n",
        "              prompt=prompt,\n",
        "              max_tokens=200,\n",
        "              n=1,\n",
        "              stop=None,\n",
        "              temperature=0.5\n",
        "            )\n",
        "            tasks.append((persona, question, task))\n",
        "    for persona, question, task in tasks:\n",
        "        response = task.result()\n",
        "        answer = response.choices[0].text.strip()\n",
        "        results.append({\n",
        "            \"Persona\": persona,\n",
        "            \"Question\": question,\n",
        "            \"Answer\": answer\n",
        "        })\n",
        "\n",
        "# Collate everything into a neatly organized dataframe\n",
        "df = pd.DataFrame(results)\n",
        "print(df)\n"
      ],
      "metadata": {
        "id": "A3I-rXte9q82"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df.to_csv(\"buyer_journey11.csv\")"
      ],
      "metadata": {
        "id": "0iVAMPu9_m-h"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}